#!/usr/bin/env python

from kivy.logger import Logger
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelHeader

from amgineirc.view.messageView import MessageView
from amgineirc.viewModel.channels.connectionsViewModel import ConnectionsViewModel
from amgineirc.view.channels.tabbedPanelHeaderView import *
from amgineirc.view.channels.defaultView import DefaultView
from amgineirc.view.channels.serverView import ServerView
from amgineirc.view.channels.channelView import ChannelView
from amgineirc.view.channels.prvmsgView import PrvmsgView
from amgineirc.view.channels.dccView import DccView
from amgineirc.view.wordWrapLabel import WordWrapLabel


class ConnectionsView(TabbedPanel, MessageView):
    """a view for all of the connection pages
    """

    def __init__(self, appdata, **kargs):
        self.view_model = None
        TabbedPanel.__init__(self, **kargs)
        MessageView.__init__(self)
        self._appdata = appdata
        self._build_default_tab()
        self.view_model = ConnectionsViewModel(self, appdata)
        # no pages because each page is an irc connection
        
    def _build_default_tab(self):
        self.default_tab_text = 'Connect'
        layout = BoxLayout()
        self.default_tab_content = DefaultView(self._appdata, size_hint=(1.0, 1.0))
        
    def switch_to(self, header):
        if self.view_model is not None:
            # show the tab label as no longer updated
            self.view_model.page_unupdated(header)
            TabbedPanel.switch_to(self, header)
            
    def remove(self, header):
        self.remove_widget(header)

    def add_server_connection(self, name, server):
        # add the server connection tab
        content = ServerView(server, self._appdata)
        th = ServerTabbedPanelHeaderView(self, server, text=name)
        th.content = content
        self.add_widget(th)
        return th

    def add_channel_connection(self, name, connection, target):
        # add the channel or nick connection tab
        content = ChannelView(connection, target, self._appdata)
        th = ChannelTabbedPanelHeaderView(self, connection.server, target, text=name)
        th.content = content
        self.add_widget(th)
        return th

    def add_prvmsg_connection(self, name, connection, target, trusted_user_record):
        # add the channel or nick connection tab
        content = PrvmsgView(connection, target, trusted_user_record, self._appdata)
        th = PrivmsgTabbedPanelHeaderView(self, connection.server, target, text=name)
        th.content = content
        self.add_widget(th)
        return th

    def add_dcc_connection(self, name, connection, nick, trusted_user_record):
        # add the channel or nick connection tab
        content = DccView(connection,
                          nick,
                          trusted_user_record,
                          self._appdata)
        th = DccTabbedPanelHeaderView(self, connection.peeraddress, nick, text=name)
        th.content = content
        self.add_widget(th)
        return th


